package com.qingcloud.adminbackend.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class PinYinUtil {


    private static final Map<String, String> DICTIONARY = new HashMap<>();

    static {
        DICTIONARY.put("覃", "qin");
        DICTIONARY.put("冯", "feng");
        DICTIONARY.put("缪", "miao");
        DICTIONARY.put("夏", "xia");
        DICTIONARY.put("瞿", "qu");
        DICTIONARY.put("曾", "zeng");
        DICTIONARY.put("石", "shi");
        DICTIONARY.put("解", "xie");
        DICTIONARY.put("折", "she");
        DICTIONARY.put("那", "na");
        DICTIONARY.put("藏", "zang");
        DICTIONARY.put("褚", "chu");
        DICTIONARY.put("扎", "zha");
        DICTIONARY.put("景", "jing");
        DICTIONARY.put("翟", "zhai");
        DICTIONARY.put("都", "du");
        DICTIONARY.put("六", "lu");
        DICTIONARY.put("薄", "bo");
        DICTIONARY.put("郇", "xun");
        DICTIONARY.put("晁", "chao");
        DICTIONARY.put("贲", "ben");
        DICTIONARY.put("贾", "jia");
        DICTIONARY.put("的", "de");
        DICTIONARY.put("馮", "feng");
        DICTIONARY.put("哈", "ha");
        DICTIONARY.put("居", "ju");
        DICTIONARY.put("尾", "wei");
        DICTIONARY.put("盖", "gai");
        DICTIONARY.put("查", "zha");
        DICTIONARY.put("盛", "sheng");
        DICTIONARY.put("塔", "ta");
        DICTIONARY.put("和", "he");
        DICTIONARY.put("柏", "bai");
        DICTIONARY.put("朴", "piao");
        DICTIONARY.put("蓝", "lan");
        DICTIONARY.put("牟", "mou");
        DICTIONARY.put("殷", "yin");
        DICTIONARY.put("谷", "gu");
        DICTIONARY.put("乾", "qian");
        DICTIONARY.put("陆", "lu");
        DICTIONARY.put("乜", "nie");
        DICTIONARY.put("乐", "le");
        DICTIONARY.put("陶", "tao");
        DICTIONARY.put("阚", "kan");
        DICTIONARY.put("叶", "ye");
        DICTIONARY.put("强", "qiang");
        DICTIONARY.put("不", "fou");
        DICTIONARY.put("丁", "ding");
        DICTIONARY.put("阿", "a");
        DICTIONARY.put("汤", "tang");
        DICTIONARY.put("万", "wan");
        DICTIONARY.put("车", "che");
        DICTIONARY.put("称", "chen");
        DICTIONARY.put("沈", "shen");
        DICTIONARY.put("区", "ou");
        DICTIONARY.put("仇", "qiu");
        DICTIONARY.put("宿", "su");
        DICTIONARY.put("南", "nan");
        DICTIONARY.put("单", "shan");
        DICTIONARY.put("卜", "bu");
        DICTIONARY.put("鸟", "niao");
        DICTIONARY.put("思", "si");
        DICTIONARY.put("寻", "xun");
        DICTIONARY.put("於", "yu");
        DICTIONARY.put("烟", "yan");
        DICTIONARY.put("余", "yu");
        DICTIONARY.put("浅", "qian");
        DICTIONARY.put("艾", "ai");
        DICTIONARY.put("浣", "wan");
        DICTIONARY.put("无", "wu");
        DICTIONARY.put("信", "xin");
        DICTIONARY.put("許", "xu");
        DICTIONARY.put("齐", "qi");
        DICTIONARY.put("俞", "yu");
        DICTIONARY.put("若", "ruo");
        DICTIONARY.put("曾", "zeng");
        DICTIONARY.put("重", "chong");
    }

    /**
     * 获取汉字串拼音首字母，英文字符不变
     *
     * @param chinese 汉字串
     * @return 汉语拼音首字母
     */
    public static String cn2FirstSpell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                    if (_t != null) {
                        pybf.append(_t[0].charAt(0));
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString().replaceAll("\\W", "").trim();
    }

    /**
     * 获取汉字串拼音，英文字符不变
     *
     * @param chinese 汉字串
     * @return 汉语拼音
     */
    public static String cn2Spell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    String[] strings = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                    if (i == 0 && strings.length != 1) {
                        //多音字
                        String s = DICTIONARY.get(String.valueOf(arr[i]));
                        if (!StringUtils.isEmpty(s)) {
                            pybf.append(s);
                        } else {
                            pybf.append(strings[0]);
                        }
                    } else if (strings == null || strings.length <= 0) {
                        pybf.append(arr[i]);
                    } else {
                        pybf.append(strings[0]);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString();
    }
}
